package com.webridge.plm.queue;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.rmi.RemoteException;
import java.sql.Timestamp;





import java.util.Calendar;
import java.util.TimeZone;

import com.webridge.plm.test.DateTest;

import wt.method.RemoteAccess;
import wt.method.RemoteMethodServer;
import wt.org.WTPrincipal;
import wt.org.WTPrincipalReference;
import wt.queue.QueueHelper;
import wt.queue.ScheduleQueue;
import wt.queue.WtQueue;
import wt.scheduler.ScheduleItem;
import wt.scheduler.SchedulingHelper;
import wt.session.SessionHelper;
import wt.util.WTException;

public class CreateScheduleQueue2 implements RemoteAccess{
	
	public static void main(String[] args) throws WTException, RemoteException, InvocationTargetException {
		RemoteMethodServer.getDefault().invoke("createScheduleQueue", CreateScheduleQueue2.class.getName(), null, new Class[]{}, new Object[]{});
	}
	
	public static void createScheduleQueue() throws WTException{
        Timestamp time = new Timestamp(System.currentTimeMillis());
        //从配置文件读取序列开始执行时间

        long spaceTime = 86400;
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        calendar.set(Calendar.HOUR_OF_DAY, 13);
        calendar.set(Calendar.MINUTE , 06);
        calendar.set(Calendar.SECOND , 00);
        Timestamp ts = new Timestamp(calendar.getTimeInMillis());
        System.out.println("ts:" + ts);
        
        createQueue("PrintDateQueue",DateTest.class.getName(),"printDate",ts,spaceTime);
    }
	
	/**
	 * 创建队列
	 * @param queueName
	 * @param className
	 * @param methodName
	 * @param startTime
	 * @param spaceTime
	 * @throws WTException
	 */
	public static void createQueue(String queueName,String className,String methodName,Timestamp startTime,long spaceTime) throws WTException {
		WtQueue queue = QueueHelper.manager.getQueue(queueName, ScheduleQueue.class);
		if (queue != null) {
			ScheduleQueue scheduleQueue = (ScheduleQueue) queue;
			Integer i = scheduleQueue.getTotalEntryCount();
			if (i > 0) {
				System.out.println("已存在相关队列条目！！");
				return;
			}
		}
		//创建计划队列对象
		ScheduleItem scheduleItem = ScheduleItem.newScheduleItem();
		//设置该队列的描述信息，随便写
		scheduleItem.setItemDescription("测试时间周期");
		//设置队列的名称，一般英文，随便写
		scheduleItem.setQueueName(queueName);
		//设置要执行的类和方法
		//类名
		scheduleItem.setTargetClass(className);
		//方法名
		scheduleItem.setTargetMethod(methodName);
		scheduleItem.setToBeRun(-1l);
		
		scheduleItem.setStartDate(startTime);
		//设置执行周期，这里设置为每24Hour执行一次(需要转换成秒),***注意是长整形的***
		scheduleItem.setPeriodicity(spaceTime);
		//设置执行该任务的用户，如果是操作Windchill对象的话会牵扯到权限控制，这里设置为管理员执行
		WTPrincipal admin = SessionHelper.manager.getAdministrator();
		scheduleItem.setPrincipalRef(WTPrincipalReference.newWTPrincipalReference(admin));
		//将计划任务对象添加到队列
		scheduleItem = SchedulingHelper.service.addItem(scheduleItem, null);
		System.out.println("计划执行队列创建完毕。。。启动时间为" + scheduleItem.getStartDate() + "，间隔时间为" + scheduleItem.getPeriodicity());
	}

}
